package com.opd.soeghtool;

import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.media.MediaRouter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.net.Socket;
import java.util.HashMap;

public class AdvDebugActivity extends Activity {

    final String TAG = "SOEADVDBG";
    LinearLayout ll_adv_debug_msg;
    String strTextView;
    GlobalVar mGlobalVar;
    TextView tv_adv_debug_msg1,tv_adv_debug_msg2;
    byte[] g_u8DataBuf;
    Button tv_adv_debug_exit,btn_adv_debug_connect,btn_adv_debug_update,btn_adv_debug_log_sw,tv_adv_debug_udp_send,tv_adv_debug_udp_recv,btn_adv_debug_cmd,btn_adv_debug_tcp_cnt,btn_adv_debug_tcp_dcn,btn_adv_debug_tcp_send,btn_adv_debug_get_output_status;
    EditText et_adv_debug_ip,et_adv_debug_port,et_adv_debug_send_content;
    UdpSocketHelper udpSocketHelper;
    TcpSocketHelper tcpSocketHelper;
    Socket socket = null;        //定义socket
    Handler handler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.advance_debug_layout);
        mGlobalVar = (GlobalVar)getApplication();
        ll_adv_debug_msg = (LinearLayout)findViewById(R.id.ll_adv_debug_msg);
        tv_adv_debug_msg1 =  (TextView)findViewById(R.id.tv_adv_debug_msg1);
        tv_adv_debug_msg2 =  (TextView)findViewById(R.id.tv_adv_debug_msg2);
        tv_adv_debug_exit =  (Button) findViewById(R.id.tv_adv_debug_exit);
        btn_adv_debug_connect =  (Button) findViewById(R.id.btn_adv_debug_connect);
        btn_adv_debug_update =  (Button) findViewById(R.id.btn_adv_debug_update);
        btn_adv_debug_log_sw =  (Button) findViewById(R.id.btn_adv_debug_log_sw);
        tv_adv_debug_udp_send =  (Button) findViewById(R.id.tv_adv_debug_udp_send);
        tv_adv_debug_udp_recv =  (Button) findViewById(R.id.tv_adv_debug_udp_recv);
        btn_adv_debug_cmd =  (Button) findViewById(R.id.btn_adv_debug_cmd);
        btn_adv_debug_tcp_cnt =  (Button) findViewById(R.id.btn_adv_debug_tcp_cnt);
        btn_adv_debug_tcp_dcn =  (Button) findViewById(R.id.btn_adv_debug_tcp_dcn);
        btn_adv_debug_tcp_send =  (Button) findViewById(R.id.btn_adv_debug_tcp_send);
        et_adv_debug_ip =  (EditText) findViewById(R.id.et_adv_debug_ip);
        et_adv_debug_port =  (EditText) findViewById(R.id.et_adv_debug_port);
        et_adv_debug_send_content =  (EditText) findViewById(R.id.et_adv_debug_send_content);
        btn_adv_debug_get_output_status =  (Button) findViewById(R.id.btn_adv_debug_get_output_status);
        udpSocketHelper = new UdpSocketHelper(4000,4400);
        g_u8DataBuf = new byte[Transfer_Con.HID_PACKET_SIZE];
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getRealMetrics(dm);
        btn_adv_debug_log_sw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mGlobalVar.getIsLogOn())
                {
                    mGlobalVar.setIsLogOn(false);
                    Toast.makeText(AdvDebugActivity.this,"关闭LOG",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    mGlobalVar.setIsLogOn(true);
                    Toast.makeText(AdvDebugActivity.this,"打开LOG",Toast.LENGTH_SHORT).show();
                }
            }
        });
        tv_adv_debug_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        strTextView = String.format("Android API: %d\n", Build.VERSION.SDK_INT);
        strTextView += String.format("Phone Resolution: %dX%d\n",dm.heightPixels,dm.widthPixels);

        et_adv_debug_ip.setInputType(InputType.TYPE_CLASS_NUMBER);
        String digits = "0123456789.";
        et_adv_debug_ip.setKeyListener(DigitsKeyListener.getInstance(digits));
        //Usb
        if(mGlobalVar.myUsbManager!=null) {
            strTextView += "Usb Mng: " + mGlobalVar.myUsbManager.toString() + "\n";
            HashMap<String, UsbDevice> deviceList = mGlobalVar.myUsbManager.getDeviceList();
            if (!deviceList.isEmpty()) {
                StringBuffer sb = new StringBuffer();
                for (UsbDevice device : deviceList.values()) {
                    sb.append(device.toString());
                    sb.append("\n");
                    // 枚举到设备
                    //Log.d(TAG,String.format("vid=%x,Pid=%x",device.getVendorId(),device.getProductId()));
                    strTextView += "Usb: " + String.format("Vid=%x,Pid=%x",device.getVendorId(),device.getProductId()) + "\n";
                    //strTextView += "Usb: " + sb.toString() + "\n";
                }

            } else {
                strTextView += "Usb: deviceList empty!" + "\n";
            }
        }
        if(mGlobalVar.myUsbDevice!=null)
            strTextView+= "\nUsb Dev: "+mGlobalVar.myUsbDevice.toString()+"\n";
        if(mGlobalVar.myInterface!=null)
            strTextView+= "\nUsb Itf: "+mGlobalVar.myInterface.toString()+"\n";
        if(mGlobalVar.epOut!=null)
            strTextView+= "\nUsb epOut: "+mGlobalVar.epOut.toString()+"\n";
        if(mGlobalVar.epIn!=null)
            strTextView+= "\nUsb epInt: "+mGlobalVar.epIn.toString()+"\n";

        if(mGlobalVar.getbIsUsbConnect())
            strTextView+= "\nUsb Cnt: True \n";
        else
            strTextView+= "\nUsb Cnt: false \n";

        if(mGlobalVar.getbIsTransferMode())
            strTextView+= "\nUsb Mode: Transfer \n";
        else
            strTextView+= "\nUsb Mode: ISP \n";
        //Variable
        strTextView+= "Game support Total: "+String.valueOf(mGlobalVar.getGameGpTotalNum())+"\n";
        for(int a=0;a<mGlobalVar.getGameGpTotalNum();a++)
        {
            strTextView+= String.format("packName[%d]: %s\n",a,mGlobalVar.getPackageName(a));
        }
        strTextView+= "User Game Num: "+String.valueOf(mGlobalVar.getUserGameNum())+"\n";
        for(int a=0;a<mGlobalVar.getUserGameNum();a++)
        {
            strTextView+= String.format("UsrGmNm[%d]: %s\n",a,mGlobalVar.getUserGameName(a));
        }
        strTextView+= "List Game Num: "+String.valueOf(mGlobalVar.getGameListTotalNum())+"\n";
        strTextView+= "def exist Game Num: "+String.valueOf(mGlobalVar.getGameDefExist())+"\n";

        // game pad
        int temp=0;
        temp = mGlobalVar.get_Version(g_u8DataBuf);
        if(temp>0)
        {
            byte[]data = new byte[temp];
            System.arraycopy(g_u8DataBuf,0,data,0,temp);
            //mGlobalVar.setStrFwVer(new String(data));
            strTextView+= "FW Version : "+new String(data)+"\n";
        }
        else
        {
            strTextView+= "FW Version : Unknown\n";
        }

        temp = mGlobalVar.get_BuildDate(g_u8DataBuf);
        if(temp>0)
        {
            byte[]data = new byte[temp];
            System.arraycopy(g_u8DataBuf,0,data,0,temp);
            strTextView+= "FW Version : "+new String(data)+"\n";
        }
        else
            strTextView+= "FW BuildDate : Unknown\n";

        if(mGlobalVar.getIsCurDeviceGamePad()) {
            if (mGlobalVar.tranGet_Value((Transfer_Con.HID_CMD_GET_TEST_STAGE), 0, 0, g_u8DataBuf)) {
                strTextView += "gp Test : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp Test : Unknown\n";

            if (mGlobalVar.tranGet_Value((Transfer_Con.HID_CMD_GET_SER_NUM), 0, 0, g_u8DataBuf)) {
                int serailNum = 0, date = 0;
                for (int a = 0; a < 4; a++) {
                    serailNum = serailNum + ((int) (g_u8DataBuf[a] & 0xff) << (a * 8));
                    date = date + ((int) (g_u8DataBuf[4 + a] & 0xff) << (a * 8));
                    Log.d(TAG, "ser:" + String.valueOf(g_u8DataBuf[a]) + " a=" + String.valueOf(serailNum));
                }

                strTextView += "gp Serial Num : " + String.valueOf(serailNum) + " at " + String.valueOf(date) + "\n";
            } else
                strTextView += "gp Serial Num : Unknown\n";


            if (mGlobalVar.get_CurPhone(g_u8DataBuf)) {
                // = g_u8DataBuf[0];
                strTextView += "gp Cur Phone : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else {
                strTextView += "gp Cur Phone : Unknown\n";
            }

            if (mGlobalVar.tranGet_Value((Transfer_Con.HID_CMD_GET_GAME_MAX), 0, 0, g_u8DataBuf)) {
                strTextView += "gp game max : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp game max : Unknown\n";

            if (mGlobalVar.get_CurGame(g_u8DataBuf)) {
                strTextView += "gp cur game : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp cur game : Unknown\n";

            if (mGlobalVar.tranGet_Value((byte) (Transfer_Con.HID_CMD_GET_LEFT_HSEN), 0, 0, g_u8DataBuf)) {
                strTextView += "gp l rocker h : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp l rocker h : Unknown\n";
            if (mGlobalVar.tranGet_Value((byte) (Transfer_Con.HID_CMD_GET_LEFT_VSEN), 0, 0, g_u8DataBuf)) {
                strTextView += "gp l rocker v : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp l rocker v : Unknown\n";
            if (mGlobalVar.tranGet_Value((byte) (Transfer_Con.HID_CMD_GET_R_ROCK_MIN), 0, 0, g_u8DataBuf)) {
                strTextView += "gp r rocker h : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp r rocker h : Unknown\n";
            if (mGlobalVar.tranGet_Value((byte) (Transfer_Con.HID_CMD_GET_R_ROCK_MAX), 0, 0, g_u8DataBuf)) {
                strTextView += "gp r rocker v : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp r rocker v : Unknown\n";

            if (mGlobalVar.tranGet_Value((byte) (Transfer_Con.HID_CMD_GET_HW_SETTING), 0, mGlobalVar.getIsSupportMenuKey() ? 0x80 : 0, g_u8DataBuf)) {
                strTextView += "gp wzry skill : " + String.valueOf(g_u8DataBuf[0]) + "\n";
            } else
                strTextView += "gp wzry skill : Unknown\n";

            if (mGlobalVar.tranGet_Value((byte) (Transfer_Con.HID_CMD_GET_PTTBL_TYPE), 0, 0, g_u8DataBuf)) {
                strTextView += "gp PtTbl from : " + ((g_u8DataBuf[0] == 0) ? "default " : "User ") + "\n";
            } else
                strTextView += "gp PtTbl from : Unknown\n";


            if (mGlobalVar.get_CurPtTbl((byte) (mGlobalVar.getDeviceMaxButton() * 2), (byte) 0, g_u8DataBuf)) {
                short a;
                int[] ptTbl = new int[mGlobalVar.getDeviceMaxButton() * 2];
                String str = "";
                for (a = 0; a < mGlobalVar.getDeviceMaxButton(); a++) {
                    ptTbl[a] = (int) (g_u8DataBuf[2 * a + 1] & 0xff) << 8;
                    ptTbl[a] += (int) (g_u8DataBuf[2 * a] & 0xff);
                    if (a % 2 == 0)
                        str += "[" + Transfer_Con.tButtonName[a / 2] + "]: ";
                    str += String.valueOf(ptTbl[a]) + ", ";
                }
                //Log.d(TAG,str);
                if (mGlobalVar.get_CurPtTbl((byte) (mGlobalVar.getDeviceMaxButton() * 2), (byte) 1, g_u8DataBuf)) {
                    //str="";
                    for (a = 0; a < mGlobalVar.getDeviceMaxButton(); a++) {
                        ptTbl[mGlobalVar.getDeviceMaxButton() + a] = (int) (g_u8DataBuf[2 * a + 1] & 0xff) << 8;
                        ptTbl[mGlobalVar.getDeviceMaxButton() + a] += (int) (g_u8DataBuf[2 * a] & 0xff);
                        if (a % 2 == 0)
                            str += "[" + Transfer_Con.tButtonName[(mGlobalVar.getDeviceMaxButton() + a) / 2] + "]: ";
                        str += String.valueOf(ptTbl[a]) + ", ";
                    }
                    //Log.d(TAG,str);
                }
                strTextView += "gp PtTbl : " + str + "\n";
            } else
                strTextView += "gp Load PtTbl fail !\n";
        }
        tv_adv_debug_msg1.setText(strTextView);

        strTextView = "";
        btn_adv_debug_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                strTextView +="enter onClick \n";
                if((mGlobalVar.epIn!=null)&&(mGlobalVar.epOut!=null))
                {
                    strTextView +="ep ok \n";
                    if(mGlobalVar.myUsbDevice.toString().contains("1105")&&mGlobalVar.myUsbDevice.toString().contains("49427"))
                    {
                        strTextView +="UsbDevice ok \n";
                        int temp=0;
                        strTextView += mGlobalVar.get_Version_test(g_u8DataBuf);

                    }
                }
                strTextView +="out onclick \n";
                tv_adv_debug_msg2.setText(strTextView );
            }
        });

        tv_adv_debug_udp_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                byte []data = new byte[3];
                data[0] = 0x1E;
                data[1] = (byte)(mGlobalVar.getScreenWidth()&0xff);
                data[2] = (byte)((mGlobalVar.getScreenWidth()>>8)&0xff);
                udpSocketHelper.sendCmd(data,3);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        tv_adv_debug_udp_recv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(udpSocketHelper.getStopUdpRecvThread())
                    udpSocketHelper.SetStopUdpRecvThread(false);
                else
                {
                    udpSocketHelper.receiveMessage(new UdpSocketHelper.OnRecvListener() {
                        @Override
                        public void onDataReceive(int len, byte[] data) {

                        }
                    });
                }
            }
        });

        btn_adv_debug_cmd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cmd="cd /data/local/tmp";
                String str = new ExeCommand().run(cmd, 10000).getResult();
                Log.i(TAG, str);
                Toast.makeText(AdvDebugActivity.this, str,Toast.LENGTH_SHORT).show();
            }
        });

        btn_adv_debug_tcp_cnt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                tcpSocketHelper= new TcpSocketHelper(et_adv_debug_ip.getText().toString(),Integer.parseInt(et_adv_debug_port.getText().toString()));
                tcpSocketHelper.connectSocket();
                //tcpSocketHelper.tcpConnectToHost();
                //tcpSocketHelper.sendMessage();
                tcpSocketHelper.receiveMessage(new TcpSocketHelper.OnReceiveListener() {
                    @Override
                    public void onDataReceive(byte[] data, int len) {
                        injectStateMachine(data, len);
                    }
                });
            }
        });

        btn_adv_debug_tcp_dcn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tcpSocketHelper.disconnectSocket();
            }
        });

        btn_adv_debug_tcp_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                injectState = 1;
                String str = "host::features=cmd,shell_v2";
                tcpSocketHelper.sendMessage(Transfer_Con.A_CNXN,0x01000000,0x00040000,str.length(),str.getBytes());
            }
        });

        btn_adv_debug_get_output_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaRouter router = (MediaRouter)getSystemService(MEDIA_ROUTER_SERVICE);
                if(router!=null) {
                    CharSequence strName = router.getDefaultRoute().toString();
                    Toast.makeText(AdvDebugActivity.this,strName,Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    int U8ToU32(byte [] data,int start)
    {
        int ret = (data[start]&0xff);
        ret += (data[start+1]&0xff)<<8;
        ret += (data[start+2]&0xff)<<16;
        ret += (data[start+3]&0xff)<<24;
        return ret;
    }

    boolean bAlreadySendSignatureKey = false,bSendAck =false;
    int mDeviceID1=0,mDeviceID2=0;
    int injectState=0;
    void injectStateMachine(byte[] data,int len)
    {
        int cmd = U8ToU32(data,0);
        int arg0 = U8ToU32(data,4);
        int arg1 = U8ToU32(data,8);
        int length = U8ToU32(data,12);
        Log.d(TAG,String.format("cmd=0x%x, arg0=0x%x, arg1=0x%x",cmd,arg0,arg1));

        for(;(length+24)<len;)
        {
            switch (cmd)
            {
                case Transfer_Con.A_CNXN:
                    String str = "shell：";
                    tcpSocketHelper.sendMessage(Transfer_Con.A_OPEN,0x00000004,0x00000000,str.length(),str.getBytes());
                    break;
                case Transfer_Con.A_AUTH:
                    if (arg0 == Transfer_Con.ADB_AUTH_TOKEN) {
                        if(!bAlreadySendSignatureKey) {

                            // byte[] key = Transfer_Con.public_key.getBytes();

                            byte[] key = new byte[257];
                            for(int k=0;k<256;k++)
                                key[k] = (byte)Transfer_Con.signature_key[k];
                            tcpSocketHelper.sendMessage(Transfer_Con.A_AUTH, Transfer_Con.ADB_AUTH_SIGNATURE, 0, 257, key);
                            bAlreadySendSignatureKey = true;
                        }
                        else
                        {
                            byte[] key = Transfer_Con.public_key.getBytes();
                            byte[] key1 = new byte[Transfer_Con.public_key.length()+1];
                            System.arraycopy(key,0,key1,0,Transfer_Con.public_key.length());
                            key1[Transfer_Con.public_key.length()]=0;
                            tcpSocketHelper.sendMessage(Transfer_Con.A_AUTH, Transfer_Con.ADB_AUTH_RSAPUBLICKEY, 0, Transfer_Con.public_key.length()+1,key1);
                            bAlreadySendSignatureKey = false;
                        }
                    }
                    break;
                case Transfer_Con.A_WRTE:
                    mDeviceID1 = arg1;
                    mDeviceID2 = arg0;
                    tcpSocketHelper.sendMessage(Transfer_Con.A_OKAY,mDeviceID1,mDeviceID2,0,null);
                    doInjectCommand();
                    break;
                case Transfer_Con.A_OKAY:
                    if(!bSendAck) {
                        byte[] ack = {0x05, 0xb, 0x00, 0x00, 0x00, 0x33, 0x30, 0x78, 0x31, 0x32, 0x30, 0x2c, 0x30, 0x78, 0x30, 0x00};
                        mDeviceID1 = arg1;
                        mDeviceID2 = arg0;
                        tcpSocketHelper.sendMessage(Transfer_Con.A_WRTE, mDeviceID1, mDeviceID2, 16, ack);
                        bSendAck = true;
                    }
                    else
                    {
                        mDeviceID1 = arg1;
                        mDeviceID2 = arg0;
                        bSendAck = false;
                        doInjectCommand();
                    }
                    break;
                case Transfer_Con.A_CLSE:
                    mDeviceID1 = arg1;
                    mDeviceID2 = arg0;
                    tcpSocketHelper.sendMessage(Transfer_Con.A_CLSE,mDeviceID1,mDeviceID2,0,null);
                    doInjectCommand();
                    break;
                default:
                    break;
            };
            cmd = U8ToU32(data,length);
            arg0 = U8ToU32(data,length+4);
            arg1 = U8ToU32(data,length+8);
            length += (U8ToU32(data,length+12)+24);
        }

        switch (cmd)
        {
            case Transfer_Con.A_CNXN:
                String str = "shell:\0";
                //String str = "shell: rm -rf /data/local/tmp/unicorn_server\n";
                //String str = "shell: cp /sdcard/SOEGHTool/server/unicorn_server /data/local/tmp/unicorn_server\n";

                tcpSocketHelper.sendMessage(Transfer_Con.A_OPEN,0x00000004,0x00000000,str.length(),str.getBytes());
                injectState = 1;
                break;
            case Transfer_Con.A_AUTH:
                if (arg0 == Transfer_Con.ADB_AUTH_TOKEN) {
                    if(!bAlreadySendSignatureKey) {

                       // byte[] key = Transfer_Con.public_key.getBytes();

                        byte[] key = new byte[257];
                        for(int k=0;k<256;k++)
                            key[k] = (byte)Transfer_Con.signature_key[k];
                        tcpSocketHelper.sendMessage(Transfer_Con.A_AUTH, Transfer_Con.ADB_AUTH_SIGNATURE, 0, 257, key);
                        bAlreadySendSignatureKey = true;
                    }
                    else
                    {
                        byte[] key = Transfer_Con.public_key.getBytes();
                        byte[] key1 = new byte[Transfer_Con.public_key.length()+1];
                        System.arraycopy(key,0,key1,0,Transfer_Con.public_key.length());
                        key1[Transfer_Con.public_key.length()]=0;
                        tcpSocketHelper.sendMessage(Transfer_Con.A_AUTH, Transfer_Con.ADB_AUTH_RSAPUBLICKEY, 0, Transfer_Con.public_key.length()+1,key1);
                        bAlreadySendSignatureKey = false;
                    }
                }
                break;
            case Transfer_Con.A_WRTE:
                mDeviceID1 = arg1;
                mDeviceID2 = arg0;
                tcpSocketHelper.sendMessage(Transfer_Con.A_OKAY,mDeviceID1,mDeviceID2,0,null);
                doInjectCommand();
                break;
            case Transfer_Con.A_OKAY:
                /*
                if(!bSendAck) {
                    byte[] ack = {0x05, 0xb, 0x00, 0x00, 0x00, 0x33, 0x30, 0x78, 0x31, 0x32, 0x30, 0x2c, 0x30, 0x78, 0x30, 0x00};
                    mDeviceID1 = arg1;
                    mDeviceID2 = arg0;
                    tcpSocketHelper.sendMessage(Transfer_Con.A_WRTE, mDeviceID1, mDeviceID2, 16, ack);
                    bSendAck = true;
                }
                else
                */
                {
                    mDeviceID1 = arg1;
                    mDeviceID2 = arg0;
                    bSendAck = false;
                    doInjectCommand();
                }
                break;
            case Transfer_Con.A_CLSE:
                mDeviceID1 = arg1;
                mDeviceID2 = arg0;
                tcpSocketHelper.sendMessage(Transfer_Con.A_CLSE,mDeviceID1,mDeviceID2,0,null);
                doInjectCommand();
                break;
            default:
                break;
        };
    }
    void doInjectCommand()
    {
        String str ="";
        switch (injectState)
        {
            case Transfer_Con.STATE_ADB_OPEN_DEV:

                injectState++;
                break;
            case Transfer_Con.STATE_ADB_REMOVE_FILE:
                str = "rm -rf /data/local/tmp/unicorn_server\n";
                tcpSocketHelper.sendMessage(Transfer_Con.A_WRTE,mDeviceID1,mDeviceID2,str.length(),str.getBytes());
                injectState++;
                break;
            case Transfer_Con.STATE_ADB_COPY_FILE:
                str = "cp /sdcard/SOEGHTool/server/unicorn_server /data/local/tmp/unicorn_server\n";
                tcpSocketHelper.sendMessage(Transfer_Con.A_WRTE,mDeviceID1,mDeviceID2,str.length(),str.getBytes());
                injectState++;
                break;
            case Transfer_Con.STATE_ADB_CHANGE_MODE:
                str = "chmod 777 /data/local/tmp/unicorn_server\n";
                tcpSocketHelper.sendMessage(Transfer_Con.A_WRTE,mDeviceID1,mDeviceID2,str.length(),str.getBytes());
                injectState++;
                break;
            case Transfer_Con.STATE_ADB_START_SERVER:
                str = "./data/local/tmp/unicorn_server &\n";
                tcpSocketHelper.sendMessage(Transfer_Con.A_WRTE,mDeviceID1,mDeviceID2,str.length(),str.getBytes());
                injectState++;
                break;
            default:
                break;
        }

    }
}
